package com.wbao.dianniu.ui;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.customView.CustomDialog;
import com.wbao.dianniu.customView.LoadingWaitDialog;
import com.wbao.dianniu.im.LoginSampleHelper;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.logical.SharedPreferencesUtils;
import com.wbao.dianniu.manager.ShareSDKManager;
import com.wbao.dianniu.update.UpdateManager;
import com.wbao.dianniu.update.UpdateManager.ICheckCompletedListener;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.R;

/**
 * 设置
 * 
 * @author yejianjun
 * 
 */
public class SettingActivity extends BaseActivity implements ICheckCompletedListener  {

	public RelativeLayout rl_account, rl_privacy, rl_base, rl_about, rl_exist;
	private RelativeLayout rl_feedback;
	AlertDialog alertDialog;
	private RelativeLayout rl_checkUpdate;
	private RelativeLayout rl_invite;
	private RelativeLayout rl_share;
	 private Dialog pDialog;
	 private UpdateManager manager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.activity_setting);
		setTitleName(getResources().getString(R.string.setting));
		init();
		manager = new UpdateManager(this);
		manager.addCheckListener(this);
		LoginSampleHelper.getInstance().getInfos(GlobalContext.environment + GlobalContext.accountId);
	}

	private void init() {
		rl_account = (RelativeLayout) findViewById(R.id.rl_account);
		rl_privacy = (RelativeLayout) findViewById(R.id.rl_privacy);
		rl_base = (RelativeLayout) findViewById(R.id.rl_base);
		rl_about = (RelativeLayout) findViewById(R.id.rl_about);
		rl_feedback = (RelativeLayout) findViewById(R.id.rl_feedback);
		rl_exist = (RelativeLayout) findViewById(R.id.rl_exist);
		rl_checkUpdate = (RelativeLayout) findViewById(R.id.rl_check_update);
		rl_invite = (RelativeLayout) findViewById(R.id.rl_invite_friends);
		rl_share = (RelativeLayout) findViewById(R.id.rl_share_to_friends);

		rl_account.setOnClickListener(this);
		rl_privacy.setOnClickListener(this);
		rl_base.setOnClickListener(this);
		rl_about.setOnClickListener(this);
		rl_feedback.setOnClickListener(this);
		rl_exist.setOnClickListener(this);
		rl_checkUpdate.setOnClickListener(this);
		rl_invite.setOnClickListener(this);
		rl_share.setOnClickListener(this);
		
	}
	
	private void showWaittingDialog(){
		manager.checkUpdate();
		pDialog = LoadingWaitDialog.createLoadingDialog(this, "");
		pDialog.show();
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.rl_account:
			intent(AccountInfoActivity.class);
			break;
		case R.id.rl_privacy:
			intent(PrivacySettingActivity.class);
			break;
		case R.id.rl_base:
			intent(BaseSetting.class);
			break;
		case R.id.rl_about:
			Intent aboutIntent = new Intent();
			aboutIntent.setClass(this, AboutActivity.class);
			startActivity(aboutIntent);
			break;
		case R.id.rl_check_update:
			showWaittingDialog();
			break;
		case R.id.rl_invite_friends:// 邀请好友加入电纽
			Intent inviteIntent = new Intent();
			inviteIntent.setClass(this, InviteFriendActivity.class);
			startActivity(inviteIntent);
			break;
		case R.id.rl_share_to_friends:// 分享给好友
			ShareSDKManager.getInstance().showShare(this, "加入电纽，身价百倍！https://www.dianniu.me/down/index.html", 3, GlobalContext.cdndownUrl + "logo.png", GlobalContext.accountId);
			break;
		case R.id.rl_feedback:
			Intent feedIntent = new Intent();
			feedIntent.setClass(this, FeedbackActivity.class);
			startActivity(feedIntent);
			break;
		case R.id.rl_exist:
			CustomDialog.Builder builder = new CustomDialog.Builder(this);  
			builder.setTitle("确认退出吗？");
	        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
	            public void onClick(DialogInterface dialog, int which) {  
	                dialog.dismiss(); 
	            	SharedPreferencesUtils.init(SettingActivity.this);
	                SharedPreferencesUtils.setAutoLoginStatus(false);
	                //设置你的操作事项  
	            	LoginSampleHelper.getInstance().loginOut_Sample();
	            	LoginSampleHelper.getInstance().setIMKit(null);
	            	Intent intent = new Intent(SettingActivity.this,LoginActivity.class);
	            	intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK  | Intent.FLAG_ACTIVITY_NEW_TASK);
	            	startActivity(intent);
	            	SettingActivity.this.finish();
	            }  
	        });  
	        builder.setNegativeButton("取消",  
	                new android.content.DialogInterface.OnClickListener() {  
	                    public void onClick(DialogInterface dialog, int which) {  
	                        dialog.dismiss();  
	                    }  
	                });  
	        builder.create().show();  
			break;
		default:
			break;
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		manager.removeCheckListener();
		if(null != pDialog && pDialog.isShowing()){
			pDialog.dismiss();
		}
	}
	
	private void intent(Class<?>  cls){
		Intent intent = new Intent();
		intent.setClass(SettingActivity.this, cls);
		startActivity(intent);
	}

	@Override
	public void checkCompleted() {
		if(null != pDialog && pDialog.isShowing()){
			pDialog.dismiss();
		}
	}

	@Override
	public void noCheck() {
		Notification.toast(this, "当前版本已是最新版本");
		if(null != pDialog && pDialog.isShowing()){
			pDialog.dismiss();
		}
	}

	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
	
}
